package com.wksc.authority.util;

import com.wksc.authority.domain.Module;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Richard
 */
public class Dom4JXml implements XmlDocument {

    private static final Logger L = LoggerFactory.getLogger(Dom4JXml.class);

    @Override
    public void parserXml(String filePath) {

    }

    @Override
    public void parserXml(File file) throws FileNotFoundException {
        if (null == file) {
            throw new NullPointerException("文件为null！");
        }
        if (!file.exists() || file.isDirectory()) {
            throw new FileNotFoundException("文件："+ file.getAbsolutePath() +"不存在或文件为目录！");
        }

        parserXml(new FileInputStream(file));

    }

    @Override
    public void parserXml(InputStream inputStream) throws FileNotFoundException {
        SAXReader saxReader = new SAXReader();

        try {

            List<Module> modules = new ArrayList<>();

            Document document = saxReader.read(inputStream);
            Element rootElement = document.getRootElement();
            List elements = rootElement.elements();

            for (Object obj : elements) {
                Module module = new Module();
                Element element = (Element) obj;
                module.setName(element.attributeValue("name"));

                L.debug("根模块名称：{}", element.attributeValue("name"));
                recursiveElement(element, module);
                L.debug("===============================================");

                modules.add(module);
            }

            L.debug("整个xml文档对象信息：{}", modules.toString());

        } catch (DocumentException e) {
            L.error("处理XML文档出错！", e);
        }
    }


    public void recursiveElement(Element element, Module module) {
        List list = element.elements();

        for (Object obj : list) {
            Element e = (Element) obj;
//            if ("module".equals(element.getName())) {
//                List<Module> modules = new ArrayList<>();
//                Module m = new Module();
//                m.setName(e.attributeValue("name"));
//                modules.add(m);
//                module.setModules(modules);
//            }

            L.debug("节点名称：{}，节点属性Name：{}", e.getName(), e.attributeValue("name"));
            L.debug("e.getUniquePath()：{}", e.getUniquePath());
            if (e.elements().size() > 0) {
                recursiveElement(e, module);
            }
        }

    }

    public static void main(String[] args) throws FileNotFoundException {
        InputStream inputStream = Dom4JXml.class.getResourceAsStream("/func.xml");
        new Dom4JXml().parserXml(inputStream);

    }
}
